﻿
namespace Thor.WebRole.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Configuration;
    using System.IO;
    using System.Linq;
    using System.Web.DomainServices;
    using System.Web.Ria;
    using System.Web.Ria.Services;
    using Thor.Common;
    using Thor.Domain;
    using Thor.Domain.Configuration;

    [EnableClientAccess()]
    public class CalendarManagerDomainService : DomainService
    {
        public Calendar GetCalendar(string calendarEmailAddress)
        {
            return CalendarHelper.GetManager().GetCalendar(calendarEmailAddress);
        }

        public IQueryable<Calendar> GetCalendarsByTimeZone(string timeZone)
        {
            var calendars = CalendarHelper.GetCalendarsByTimeZone(timeZone);
            return calendars.AsQueryable();
        }

        public IQueryable<Calendar> GetCalendars()
        {
            List<Calendar> calendars = CalendarHelper.GetManager().Calendars;
            return calendars.AsQueryable();
        }

        public void InsertCalendar(Calendar calendar)
        {
            CalendarHelper.GetManager().Insert(calendar);
            AddMessageToQueue("Calendar inserted.");
        }

        public void UpdateCalendar(Calendar calendar)
        {
            CalendarHelper.GetManager().Update(calendar);
            AddMessageToQueue("Calendar updated.");
        }

        public void DeleteCalendar(Calendar calendar)
        {
            CalendarHelper.GetManager().Delete(calendar);
            AddMessageToQueue("Calendar deleted.");
        }

        private void AddMessageToQueue(string message)
        {
            var msg = new Message() { Text = message };
            var messageManager = MessageHelper.GetMessageManager();
            messageManager.Insert(msg);
        }
    }
}